package com.fmx.BookStore.controller;

import com.fmx.BookStore.core.domain.AjaxResult;
import com.fmx.BookStore.domain.Book;
import com.fmx.BookStore.service.IBookService;
import com.fmx.BookStore.service.impl.BookServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("book")
public class BookController {
    @Autowired
    private IBookService bookService;
    @GetMapping(value = "/{bookId}")
    @ApiOperation("根据ID找书籍")
    public AjaxResult getBookById(@PathVariable("bookId")Long bookId){
        return AjaxResult.success(bookService.getBookById(bookId));
    }

    @GetMapping("/list")
    @ApiOperation("根据其他信息查询书籍")
    public AjaxResult listBook(Book book){
        List<Book> list = bookService.listBook(book);
        return AjaxResult.success(list);
    }

    @PostMapping
    @ApiOperation("新增书籍")
    public AjaxResult addBook(@RequestBody Book book){
        int rows = bookService.insertBook(book);
        return rows > 0 ? AjaxResult.success("添加书籍成功") : AjaxResult.error("添加书籍失败");
    }

    @PutMapping
    @ApiOperation("修改书籍信息")
    public AjaxResult updateBook(@RequestBody Book book){
        int rows = bookService.updateBook(book);
        return rows > 0 ? AjaxResult.success("修改书籍成功") : AjaxResult.error("修改书籍失败");
    }

    @DeleteMapping("/{bookId}")
    @ApiOperation("删除书籍")
    public AjaxResult deleteBook(@PathVariable Long bookId){
        int rows = bookService.deleteBook(bookId);
        return rows > 0 ? AjaxResult.success("删除书籍成功") : AjaxResult.error("删除书籍失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Book book){
        PageHelper.startPage(pageNum,pageSize);
        List<Book> list = bookService.listBook(book);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }
}
